package com.uphone.kingmall.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.china.zhuoyue.R;
import com.uphone.kingmall.bean.CatsListBean;
import com.uphone.kingmall.utils.GlideUtils;

import java.util.List;

/**
 * 右侧主界面ListView的适配器
 *
 * @author Administrator
 */
public class ClassHomeAdapter extends BaseAdapter {


    private Context context;
    private List<CatsListBean.CatsBean.ChildrenBean> foodDatas;

    public ClassHomeAdapter(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        if (foodDatas != null) {
            return foodDatas.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return foodDatas == null ? 0 : foodDatas.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold viewHold = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.kingmall_item_home_category, null);
            viewHold = new ViewHold();
            viewHold.tv_name = (TextView) convertView.findViewById(R.id.item_home_name);
            viewHold.iv_icon = (ImageView) convertView.findViewById(R.id.item_album);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewHold.iv_icon.getLayoutParams();
            int width = (int) (((ScreenUtils.getScreenWidth() / 4d * 3) - SizeUtils.dp2px(44)) / 3);
            layoutParams.width = width;
            layoutParams.height = width;
            viewHold.iv_icon.setLayoutParams(layoutParams);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();
        }
        viewHold.tv_name.setText(foodDatas.get(position).getCatName() + "");

        GlideUtils.getInstance().loadCornerImage(context, foodDatas.get(position).getCatIcon(), SizeUtils.dp2px(2), viewHold.iv_icon);
        return convertView;


    }

    private static class ViewHold {
        private TextView tv_name;
        private ImageView iv_icon;
    }

    public void setList(List<CatsListBean.CatsBean.ChildrenBean> list) {
        this.foodDatas = list;
        notifyDataSetChanged();
    }

}
